package kr.co.icebird.spring3.app.controller;

import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import kr.co.icebird.spring3.app.model.Member;
import kr.co.icebird.spring3.app.service.MemberService;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.document.AbstractJExcelView;

@Controller
public class JFreeChartController {

	@Autowired
	MemberService memberService;

	@RequestMapping("/jfree/list.do")
	public ModelAndView jtest01(HttpServletRequest request,
			HttpServletResponse response) {

		return new ModelAndView("/jfree/jtest01", "mList",
				memberService.getMemberList());
	}

	@RequestMapping("/jfree/excel.do")
	public ModelAndView jexcel01() throws Exception {

		return new ModelAndView(new AbstractJExcelView() {

			@Override
			protected void buildExcelDocument(Map<String, Object> map,
					WritableWorkbook workbook, HttpServletRequest request,
					HttpServletResponse response) throws Exception {
				List<Member> rList = (List<Member>) map.get("data");

				if (workbook.getNumberOfSheets() == 0) {
					workbook.createSheet("sheet1", 0);
				}
				DefaultPieDataset chartDataset = new DefaultPieDataset();
				WritableSheet sheet = workbook.getSheet(0);
				WritableCellFormat numFormat = new WritableCellFormat(
						new NumberFormat("###,###,###,###"));
				int i = 0;

				for (Member member : rList) {
					int j = 0;
					sheet.addCell(new Label(j++, i, member.getName()));
					sheet.addCell(new Number(j++, i, member.getAge(), numFormat));
					sheet.addCell(new Label(j++, i, member.getAddress()));
					chartDataset.setValue(member.getName(), member.getAge());
					i++;
				}
				JFreeChart chart = ChartFactory.createPieChart("한글테스트",
						chartDataset, true, true, true);
				chart.getTitle().setFont(new Font("돋음", Font.BOLD, 20));
				BufferedImage br = chart.createBufferedImage(400, 400);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				ImageIO.write(br, "PNG", baos);
				baos.flush();
				byte[] imageInByte = baos.toByteArray();
				baos.close();
				sheet.addImage(new WritableImage(1, i + 2, 5, 10, imageInByte));

			}
		}, "data", memberService.getMemberList());
	}

	@RequestMapping("/jfree/chart01.do")
	public void chart01(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setContentType("img/png");
		DefaultPieDataset chartDataset = new DefaultPieDataset();

		for (Member member : memberService.getMemberList()) {
			chartDataset.setValue(member.getName(), member.getAge());
		}
		// pieDataset.setValue("Samsung", new Integer(20));
		// pieDataset.setValue("Sony Ericsson", new Integer(10));
		// pieDataset.setValue("LG", new Integer(10));

		JFreeChart chart = ChartFactory.createPieChart(
				"Mobile Device Pie Chart", chartDataset, true, true, true);

		ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 400,
				400);

		response.getOutputStream().close();
	}
}
